home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 115 / tgm_cdrom_115.iso / Shareware / Blender 2.12 / blender2.12-windows.exe / ic255.cab / python / ribexport.py < prev    next >
Text File  |  2001-03-15  |  16KB  |  327 lines

  1. #######################
  2. # (c) Jan Walter 2000 #
  3. #######################
  4.  
  5. # CVS
  6. # $Author: jan $
  7. # $Date: 2000/12/14 15:34:25 $
  8. # $RCSfile: ribexport.py,v $
  9. # $Revision: 1.1 $
  10.  
  11. import Blender
  12. import math
  13.  
  14. exportAnimations = 0
  15.  
  16. class RenderManExport:
  17.     def __init__(self, filename):
  18.         self.file    = open(filename, "w")
  19.         self.scene   = None
  20.         self.display = None
  21.  
  22.     def export(self, scene):
  23.         global exportAnimations
  24.  
  25.         print "exporting ..."
  26.         self.scene = scene
  27.         self.writeHeader()
  28.         self.display = Blender.getDisplaySettings()
  29.         if exportAnimations:
  30.             for frame in xrange(self.display.startFrame,
  31.                                 self.display.endFrame + 1):
  32.                 self.writeFrame(frame)
  33.         else:
  34.             self.writeFrame(self.display.currentFrame)
  35.         self.writeEnd()
  36.  
  37.     def writeCamera(self):
  38.         camobj = self.scene.getCurrentCamera()
  39.         camera = Blender.getCamera(camobj.data)
  40.         factor = self.display.yResolution / float(self.display.xResolution)
  41.         self.file.write('Projection "perspective" "fov" [%s]\n' %
  42.                         (360.0 * math.atan(factor * 16.0 / camera.Lens) /
  43.                          math.pi))
  44.         self.file.write("Clipping %s %s\n" % (camera.ClSta, camera.ClEnd))
  45.         self.file.write("Transform [" +
  46.                         "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s]\n" %
  47.                         (camobj.inverseMatrix[0][0],
  48.                          camobj.inverseMatrix[0][1],
  49.                          -camobj.inverseMatrix[0][2],
  50.                          camobj.inverseMatrix[0][3],
  51.                          camobj.inverseMatrix[1][0],
  52.                          camobj.inverseMatrix[1][1],
  53.                          -camobj.inverseMatrix[1][2],
  54.                          camobj.inverseMatrix[1][3],
  55.                          camobj.inverseMatrix[2][0],
  56.                          camobj.inverseMatrix[2][1],
  57.                          -camobj.inverseMatrix[2][2],
  58.                          camobj.inverseMatrix[2][3],
  59.                          camobj.inverseMatrix[3][0],
  60.                          camobj.inverseMatrix[3][1],
  61.                          -camobj.inverseMatrix[3][2],
  62.                          camobj.inverseMatrix[3][3]))
  63.  
  64.     def writeDisplaySettings(self, frame):
  65.         self.file.write("Format %s %s %s\n" % (self.display.xResolution,
  66.                                                self.display.yResolution,
  67.                                                self.display.pixelAspectRatio))
  68.         self.file.write('Display "%s" "file" "rgba"\n' %
  69.                         ("frame" + "%04d" % frame + ".tif"))
  70.  
  71.     def writeEnd(self):
  72.         self.file.close()
  73.         print "... finished"
  74.  
  75.     def writeFrame(self, frame):
  76.         print "frame:", frame
  77.         Blender.setCurrentFrame(frame)
  78.         self.file.write("FrameBegin %s\n" % (frame - self.display.startFrame))
  79.         self.writeDisplaySettings(frame)
  80.         self.writeCamera()
  81.         self.writeWorld()
  82.         self.file.write("FrameEnd\n")
  83.  
  84.     def writeHeader(self):
  85.         self.file.write("##RenderMan RIB-Structure 1.0\n")
  86.         self.file.write("version 3.03\n")
  87.  
  88.     def writeIdentifier(self, name):
  89.         self.file.write("%s\n" % ("#" * (len(name) + 4)))
  90.         self.file.write("# %s #\n" % name)
  91.         self.file.write("%s\n" % ("#" * (len(name) + 4)))
  92.  
  93.     def writeLamp(self, name, num):
  94.         self.writeIdentifier(name)
  95.         lampobj = Blender.getObject(name)
  96.         lamp    = Blender.getLamp(lampobj.data)
  97.         x = lampobj.matrix[3][0] / lampobj.matrix[3][3]
  98.         y = lampobj.matrix[3][1] / lampobj.matrix[3][3]
  99.         z = lampobj.matrix[3][2] / lampobj.matrix[3][3]
  100.         self.file.write('LightSource "pointlight" %s ' % num +
  101.                         '"from" [%s %s %s] ' % (x, y, z) +
  102.                         '"lightcolor" [%s %s %s] ' % (lamp.R, lamp.G, lamp.B) +
  103.                         '"intensity" 50\n')
  104.  
  105.     def writeMatrix(self, matrix):
  106.         self.file.write("Transform [" +
  107.                         "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s]\n" %
  108.                         (matrix[0][0], matrix[0][1],
  109.                          matrix[0][2], matrix[0][3],
  110.                          matrix[1][0], matrix[1][1],
  111.                          matrix[1][2], matrix[1][3],
  112.                          matrix[2][0], matrix[2][1],
  113.                          matrix[2][2], matrix[2][3],
  114.                          matrix[3][0], matrix[3][1],
  115.                          matrix[3][2], matrix[3][3]))
  116.  
  117.     def writeObject(self, name):
  118.         if Blender.isMesh(name):
  119.             self.writeIdentifier(name)
  120.             meshobj = Blender.getObject(name)
  121.             mesh    = Blender.getMesh(meshobj.data)
  122.             if mesh.texture:
  123.                 self.file.write('Surface "paintedplastic" "texturename" ' +
  124.                                 '["%s.tif"]\n' % "st")
  125.             else:
  126.                 self.file.write('Surface "plastic"\n')
  127.             self.file.write("Color [%s %s %s]\n" % (0.8, 0.8, 0.8))
  128.             self.file.write("AttributeBegin\n")
  129.             self.writeMatrix(meshobj.matrix)
  130.             index = 0
  131.             for face in mesh.faces:
  132.                 if meshobj.materials and meshobj.materials[face[5]]:
  133.                     material = Blender.getMaterial(meshobj.materials[face[5]])
  134.                     self.file.write("Color [%s %s %s]\n" %
  135.                                     (material.R, material.G, material.B))
  136.                 if face[3]:
  137.                     # quad
  138.                     if face[4]: # smooth
  139.                         # first triangle
  140.                         self.file.write('Polygon "P" [ ')
  141.                         for i in xrange(3):
  142.                             self.file.write("%s %s %s " %
  143.                                             (mesh.vertices[face[i]][0],
  144.                                              mesh.vertices[face[i]][1],
  145.                                              mesh.vertices[face[i]][2]))
  146.                         self.file.write('] "N" [ ')
  147.                         for i in xrange(3):
  148.                             self.file.write("%s %s %s " %
  149.                                             (mesh.normals[face[i]][0],
  150.                                              mesh.normals[face[i]][1],
  151.                                              mesh.normals[face[i]][2]))
  152.                         if mesh.colors:
  153.                             self.file.write('] "Cs" [ ')
  154.                             for i in xrange(3):
  155.                                 self.file.write("%s %s %s " %
  156.                                                 (mesh.colors[face[i]][0],
  157.                                                  mesh.colors[face[i]][1],
  158.                                                  mesh.colors[face[i]][2]))
  159.                         if mesh.texture:
  160.                             self.file.write('] "st" [ ')
  161.                             for i in xrange(3):
  162.                                 self.file.write("%s %s " %
  163.                                                 (mesh.texcoords[index][i][0],
  164.                                                  1.0 -
  165.                                                  mesh.texcoords[index][i][1]))
  166.                             self.file.write(']\n')
  167.                         else:
  168.                             self.file.write(']\n')
  169.                         # second triangle
  170.                         self.file.write('Polygon "P" [ ')
  171.                         for i in [0, 2, 3]:
  172.                             self.file.write("%s %s %s " %
  173.                                             (mesh.vertices[face[i]][0],
  174.                                              mesh.vertices[face[i]][1],
  175.                                              mesh.vertices[face[i]][2]))
  176.                         self.file.write('] "N" [ ')
  177.                         for i in [0, 2, 3]:
  178.                             self.file.write("%s %s %s " %
  179.                                             (mesh.normals[face[i]][0],
  180.                                              mesh.normals[face[i]][1],
  181.                                              mesh.normals[face[i]][2]))
  182.                         if mesh.colors:
  183.                             self.file.write('] "Cs" [ ')
  184.                             for i in [0, 2, 3]:
  185.                                 self.file.write("%s %s %s " %
  186.                                                 (mesh.colors[face[i]][0],
  187.                                                  mesh.colors[face[i]][1],
  188.                                                  mesh.colors[face[i]][2]))
  189.                         if mesh.texture:
  190.                             self.file.write('] "st" [ ')
  191.                             for i in [0, 2, 3]:
  192.                                 self.file.write("%s %s " %
  193.                                                 (mesh.texcoords[index][i][0],
  194.                                                  1.0 -
  195.                                                  mesh.texcoords[index][i][1]))
  196.                             self.file.write(']\n')
  197.                         else:
  198.                             self.file.write(']\n')
  199.                     else: # not smooth
  200.                         # first triangle
  201.                         self.file.write('Polygon "P" [ ')
  202.                         for i in xrange(3):
  203.                             self.file.write("%s %s %s " %
  204.                                             (mesh.vertices[face[i]][0],
  205.                                              mesh.vertices[face[i]][1],
  206.                                              mesh.vertices[face[i]][2]))
  207.                         if mesh.colors:
  208.                             self.file.write('] "Cs" [ ')
  209.                             for i in xrange(3):
  210.                                 self.file.write("%s %s %s " %
  211.                                                 (mesh.colors[face[i]][0],
  212.                                                  mesh.colors[face[i]][1],
  213.                                                  mesh.colors[face[i]][2]))
  214.                         if mesh.texture:
  215.                             self.file.write('] "st" [ ')
  216.                             for i in xrange(3):
  217.                                 self.file.write("%s %s " %
  218.                                                 (mesh.texcoords[index][i][0],
  219.                                                  1.0 -
  220.                                                  mesh.texcoords[index][i][1]))
  221.                             self.file.write(']\n')
  222.                         else:
  223.                             self.file.write(']\n')
  224.                         # second triangle
  225.                         self.file.write('Polygon "P" [ ')
  226.                         for i in [0, 2, 3]:
  227.                             self.file.write("%s %s %s " %
  228.                                             (mesh.vertices[face[i]][0],
  229.                                              mesh.vertices[face[i]][1],
  230.                                              mesh.vertices[face[i]][2]))
  231.                         if mesh.colors:
  232.                             self.file.write('] "Cs" [ ')
  233.                             for i in [0, 2, 3]:
  234.                                 self.file.write("%s %s %s " %
  235.                                                 (mesh.colors[face[i]][0],
  236.                                                  mesh.colors[face[i]][1],
  237.                                                  mesh.colors[face[i]][2]))
  238.                         if mesh.texture:
  239.                             self.file.write('] "st" [ ')
  240.                             for i in [0, 2, 3]:
  241.                                 self.file.write("%s %s " %
  242.                                                 (mesh.texcoords[index][i][0],
  243.                                                  1.0 -
  244.                                                  mesh.texcoords[index][i][1]))
  245.                             self.file.write(']\n')
  246.                         else:
  247.                             self.file.write(']\n')
  248.                 else:
  249.                     # triangle
  250.                     if face[4]: # smooth
  251.                         self.file.write('Polygon "P" [ ')
  252.                         for i in xrange(3):
  253.                             self.file.write("%s %s %s " %
  254.                                             (mesh.vertices[face[i]][0],
  255.                                              mesh.vertices[face[i]][1],
  256.                                              mesh.vertices[face[i]][2]))
  257.                         self.file.write('] "N" [ ')
  258.                         for i in xrange(3):
  259.                             self.file.write("%s %s %s " %
  260.                                             (mesh.normals[face[i]][0],
  261.                                              mesh.normals[face[i]][1],
  262.                                              mesh.normals[face[i]][2]))
  263.                         if mesh.colors:
  264.                             self.file.write('] "Cs" [ ')
  265.                             for i in xrange(3):
  266.                                 self.file.write("%s %s %s " %
  267.                                                 (mesh.colors[face[i]][0],
  268.                                                  mesh.colors[face[i]][1],
  269.                                                  mesh.colors[face[i]][2]))
  270.                         if mesh.texture:
  271.                             self.file.write('] "st" [ ')
  272.                             for i in xrange(3):
  273.                                 self.file.write("%s %s " %
  274.                                                 (mesh.texcoords[index][i][0],
  275.                                                  1.0 -
  276.                                                  mesh.texcoords[index][i][1]))
  277.                             self.file.write(']\n')
  278.                         else:
  279.                             self.file.write(']\n')
  280.                     else: # not smooth
  281.                         self.file.write('Polygon "P" [ ')
  282.                         for i in xrange(3):
  283.                             self.file.write("%s %s %s " %
  284.                                             (mesh.vertices[face[i]][0],
  285.                                              mesh.vertices[face[i]][1],
  286.                                              mesh.vertices[face[i]][2]))
  287.                         if mesh.colors:
  288.                             self.file.write('] "Cs" [ ')
  289.                             for i in xrange(3):
  290.                                 self.file.write("%s %s %s " %
  291.                                                 (mesh.colors[face[i]][0],
  292.                                                  mesh.colors[face[i]][1],
  293.                                                  mesh.colors[face[i]][2]))
  294.                         if mesh.texture:
  295.                             self.file.write('] "st" [ ')
  296.                             for i in xrange(3):
  297.                                 self.file.write("%s %s " %
  298.                                                 (mesh.texcoords[index][i][0],
  299.                                                  1.0 -
  300.                                                  mesh.texcoords[index][i][1]))
  301.                             self.file.write(']\n')
  302.                         else:
  303.                             self.file.write(']\n')
  304.                 index = index + 1
  305.             self.file.write("AttributeEnd\n")
  306.         else:
  307.             print "Sorry can export meshes only ..."
  308.  
  309.     def writeWorld(self):
  310.         self.file.write("WorldBegin\n")
  311.         self.file.write('Attribute "light" "shadows" "on"\n')
  312.         # first all lights
  313.         lamps = 0
  314.         for name in self.scene.objects:
  315.             if Blender.isLamp(name):
  316.                 lamps = lamps + 1
  317.                 self.writeLamp(name, lamps)
  318.         # now all objects which are not a camera or a light
  319.         for name in self.scene.objects:
  320.             if not Blender.isCamera(name) and not Blender.isLamp(name):
  321.                 self.writeObject(name)
  322.         self.file.write("WorldEnd\n")
  323.  
  324. ribexport = RenderManExport("test.rib")
  325. scene = Blender.getCurrentScene()
  326. ribexport.export(scene)
  327.